import {Filter} from "../../../web/_Filter"
import {Component} from "../../anno/Component"
import {Order} from "../../anno/Order"
import {LogFactory} from "../../log"


@Component()
@Order(10000)
class ResponseFallbackFilter extends Filter{
  private logger = LogFactory.getLogger("ResponseFallbackFilter")

  doFilter(req: HttpRequest, res: HttpResponse, next: (...args: any[]) => void): void {
    next()
  }

  afterThrow(req: HttpRequest, res: HttpResponse, ex: Error) {
    if (!res.closed()){
      res.end(ex.message, 500)
    }
    if (this.logger.isTraceEnabled()) {
      this.logger.trace(`fallback filter handled url - ${req.url}`)
    }
  }
}
